package com.cennavi.opers;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DataStat extends HttpServlet {
 
	private static final long serialVersionUID = 3394865757422423446L;

	public DataStat() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods",
				"POST, GET, OPTIONS, DELETE,PUT");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		try {
			
			String req = request
					.getParameter("request");
			
			req = new String(req.getBytes("ISO-8859-1"),"UTF-8");
			
			System.out.println(req);

			JSONObject json = JSONObject.fromObject(req);
			
			

			
			String id = json.getString("instruction_id");
			String sql = json.getString("instruction");
			JSONArray fileList = json.getJSONArray("file_list");
			String names = json.getString("names");
			StringBuilder files = new StringBuilder(fileList.getString(0));
			for(int i=1;i<fileList.size();i++)
				files.append(","+fileList.getString(i));

			StringBuilder sb = new StringBuilder(
					"spark-submit --master yarn --num-executors "
							+ 50
							+ " --executor-memory 8500m --class com.cennavi.engine.data_stat --name lunch --jars /var/lib/hadoop-hdfs/lilei/tools.jar,/var/lib/hadoop-hdfs/lilei/spark-csv_2.11-1.3.2.jar,/var/lib/hadoop-hdfs/lilei/commons-csv-1.1.jar /var/lib/hadoop-hdfs/lilei/engine-a.jar ");

			sb.append(files.toString() + "^");
			sb.append(names + "^");
			sb.append(sql+"^");
			
			sb.append(id);

			final String cmd = sb.toString();

			scala.io.Source.fromURL(
					"http://10.20.20.11:8080/maven-web/opers?param="
							+ URLEncoder.encode(cmd, "UTF-8"), "UTF-8");

			out.println("{\"error_code\":0,\"error_msg\":\"\"}");

		} catch (Exception e) {

			e.printStackTrace();

			out.println("{\"error_code\":-1,\"error_msg\":\"" + e.getMessage()
					+ "\"}");
		} finally {
			out.flush();

			out.close();
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
